package com.tang.controller;

import com.tang.entity.*;
import com.tang.service.BookService;
import com.tang.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author tang
 * @create 2020-09-14
 */


@WebServlet("/admin")
/**
 * 响应管理员端的需求
 */
public class AdminServlet extends HttpServlet {
    BookService bookService = new BookServiceImpl();

    /**
     * 响应借书管理，还书管理的查看与操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        HttpSession session = req.getSession();


        switch (method) {
            case "handle":
                //管理员处理借书申请,图书归还
                String idStr = req.getParameter("id");
                Integer id = Integer.parseInt(idStr);
                String stateStr = req.getParameter("state");
                Integer state = Integer.parseInt(stateStr);
                Admin admin = (Admin) session.getAttribute("admin");
                Integer adminId = admin.getId();
                bookService.hand(id,adminId,state);
                //判断处理前界面，并返回
                String beforepage = req.getParameter("before");
                if (beforepage ==null){
                    beforepage ="admin";
                }
                switch (beforepage) {
                    case "admin":
                        List<Review> list2 = bookService.reviewQuery();
                        req.setAttribute("list",list2);
                        req.getRequestDispatcher("admin.jsp").forward(req,resp);
                        break;
                    case "return":
                        List<Review> listReturn = bookService.returnQuery();
                        req.setAttribute("list",listReturn);
                        req.getRequestDispatcher("return.jsp").forward(req,resp);
                        break;
                }
                break;
            case "review":
                //管理员查看借书申请
                List<Review> listReview = bookService.reviewQuery();
                req.setAttribute("list",listReview);
                req.getRequestDispatcher("admin.jsp").forward(req,resp);
                break;
            case"return":
                //管理员查看还书申请
                List<Review> listReturn = bookService.returnQuery();
                req.setAttribute("list",listReturn);
                req.getRequestDispatcher("return.jsp").forward(req,resp);
                break;


        }
    }
}
